#include "TempVisualComponent.h"
#include "..\Managers\RenderManager.h"
#include "..\Managers\PhysicsManager.h"
#include "GameObject.h"
#include "PhysicsComponent.h"


TempVisualComponent::TempVisualComponent(unsigned int p_view, const std::string& p_stringID, const std::string& p_filepath, float p_layer, bool p_updatePosition) : m_updatePosition(p_updatePosition)
{
	m_sprite = RenderManager::getSingleton().getRenderEngine()->createSprite(p_view, p_stringID, p_layer);
	m_animation = new vn::AnimationManager(m_sprite);
	m_animation->loadAnimations(p_filepath);
	m_animation->setAnimation("default");
}

TempVisualComponent::~TempVisualComponent(void)
{
	if (m_sprite != nullptr)
	{
		PhysicsManager::getSingleton().removeSpriteSync(m_sprite);
		RenderManager::getSingleton().getRenderEngine()->destroySprite(m_sprite);
		m_sprite = nullptr;
	}
	delete m_animation;
	m_animation = nullptr;
}

void TempVisualComponent::update(const GameTime& p_gameTime)
{
	if (m_animation->isDone())
	{
		getOwner()->removeComponent(this);
	}
	m_animation->update(p_gameTime);
}

void TempVisualComponent::init()
{

}

void TempVisualComponent::updatePosition()
{
	PhysicsComponent* pc = dynamic_cast<PhysicsComponent*>(getOwner()->getComponent("PhysicsComponent"));
	if (pc)
	{
		PhysicsManager::getSingleton().addSpriteSync(pc->getBody(), m_sprite);
	}
}